#include "connectiondialog.h"
#include <QVBoxLayout>
#include <QFormLayout>
#include <QLineEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QLabel>
#include <QApplication>

ConnectionDialog::ConnectionDialog(QWidget *parent)
    : QDialog(parent)
    , m_editMode(false)
{
    setupUI();
    setupConnections();
    validateInput();
}

QString ConnectionDialog::getConnectionName() const
{
    return m_nameEdit->text();
}

QString ConnectionDialog::getDriver() const
{
    return m_driverCombo->currentText();
}

QString ConnectionDialog::getHost() const
{
    return m_hostEdit->text();
}

QString ConnectionDialog::getDatabase() const
{
    return m_databaseEdit->text();
}

QString ConnectionDialog::getUsername() const
{
    return m_usernameEdit->text();
}

QString ConnectionDialog::getPassword() const
{
    return m_passwordEdit->text();
}

int ConnectionDialog::getPort() const
{
    return m_portSpinBox->value();
}

void ConnectionDialog::setConnectionName(const QString &name)
{
    m_nameEdit->setText(name);
}

void ConnectionDialog::setDriver(const QString &driver)
{
    int index = m_driverCombo->findText(driver);
    if (index >= 0) {
        m_driverCombo->setCurrentIndex(index);
    }
}

void ConnectionDialog::setHost(const QString &host)
{
    m_hostEdit->setText(host);
}

void ConnectionDialog::setDatabase(const QString &database)
{
    m_databaseEdit->setText(database);
}

void ConnectionDialog::setUsername(const QString &username)
{
    m_usernameEdit->setText(username);
}

void ConnectionDialog::setPassword(const QString &password)
{
    m_passwordEdit->setText(password);
}

void ConnectionDialog::setPort(int port)
{
    m_portSpinBox->setValue(port);
}

bool ConnectionDialog::isEditMode() const
{
    return m_editMode;
}

void ConnectionDialog::setEditMode(bool editMode)
{
    m_editMode = editMode;
    if (m_editMode) {
        setWindowTitle(tr("Edit Connection"));
        m_nameEdit->setReadOnly(true);
    } else {
        setWindowTitle(tr("New Connection"));
        m_nameEdit->setReadOnly(false);
    }
}

void ConnectionDialog::validateInput()
{
    bool valid = !m_nameEdit->text().isEmpty() && 
                 !m_driverCombo->currentText().isEmpty();
    
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
}

void ConnectionDialog::setupUI()
{
    setWindowTitle(tr("New Connection"));
    setModal(true);
    
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    QFormLayout *formLayout = new QFormLayout;
    
    m_nameEdit = new QLineEdit;
    formLayout->addRow(tr("Connection Name:"), m_nameEdit);
    
    m_driverCombo = new QComboBox;
    m_driverCombo->addItem("QSQLITE");
    m_driverCombo->addItem("QMYSQL");
    m_driverCombo->addItem("QPSQL");
    m_driverCombo->addItem("QOCI");
    m_driverCombo->addItem("QODBC");
    m_driverCombo->addItem("QDB2");
    m_driverCombo->addItem("QIBASE");
    m_driverCombo->addItem("QTDS");
    formLayout->addRow(tr("Driver:"), m_driverCombo);
    
    m_hostEdit = new QLineEdit("localhost");
    formLayout->addRow(tr("Host:"), m_hostEdit);
    
    m_databaseEdit = new QLineEdit;
    formLayout->addRow(tr("Database:"), m_databaseEdit);
    
    m_usernameEdit = new QLineEdit;
    formLayout->addRow(tr("Username:"), m_usernameEdit);
    
    m_passwordEdit = new QLineEdit;
    m_passwordEdit->setEchoMode(QLineEdit::Password);
    formLayout->addRow(tr("Password:"), m_passwordEdit);
    
    m_portSpinBox = new QSpinBox;
    m_portSpinBox->setRange(0, 65535);
    m_portSpinBox->setValue(0);
    m_portSpinBox->setSpecialValueText(tr("Default"));
    formLayout->addRow(tr("Port:"), m_portSpinBox);
    
    m_savePasswordCheck = new QCheckBox(tr("Save password"));
    m_savePasswordCheck->setChecked(true);
    formLayout->addRow("", m_savePasswordCheck);
    
    mainLayout->addLayout(formLayout);
    
    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    mainLayout->addWidget(m_buttonBox);
    
    connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

void ConnectionDialog::setupConnections()
{
    connect(m_nameEdit, &QLineEdit::textChanged, this, &ConnectionDialog::validateInput);
    connect(m_driverCombo, &QComboBox::currentTextChanged, this, &ConnectionDialog::validateInput);
}